function tImg = cropImageForPlane(R,plane,img_path,img_name)
% R: rotation from world to volume, x^v = R * x^w
% plane: the structure defined in createTree.m

view = readView(img_path,[img_name(end-3) '.view']);
% decide if view plane is visible under camemra direction
coef = plane.nt(1:3).*view.cam_dir;
if coef<-0.6;
   tImg =[];
   return;
end

P_origin=view.P*inv(R)*plane.origin;
p0x = P_origin(1); p0y=P_origion(2);p0z=P_origin(3);

P_u = view.P*inv(R)*plane.u;
pux = P_u(1); puy =P_u(2); puz = P_u(3);

P_v = view.P*inv(r)*plane.v;
pvx = P_v(1); pvy = P_v(2); pvz = P_v(3);

%contruct H, where input_image = H * desired image
H=[pux pvx p0x;puy pvy p0y;puz pvz p0z];

% load image, and resize to annoationed image size
img = imread([img_path '/' img_name]);
if ndims(img)==3
    img = rgb2gray(img);
end
img = imresize(img,[view.height view.width]);

% transform image
tForm = maketranform('projective',inv(H)');
tImg = imtransform(img,tForm,'xdata',[1 plane.w],'ydata',[1 plane.h]);
% make sure tImg size is h*w
tImg = imresize(tImg,[plane.w plane.h]);